package com.bjpowernode.crm.service.impl;

import com.bjpowernode.crm.constants.Constants;
import com.bjpowernode.crm.mapper.*;
import com.bjpowernode.crm.pojo.*;
import com.bjpowernode.crm.service.ClueService;
import com.bjpowernode.crm.utils.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ClueServiceImpl implements ClueService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private ClueRemarkMapper clueRemarkMapper;

    @Autowired
    private ClueActivityMapper clueActivityMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CustomerRemarkMapper customerRemarkMapper;

    @Autowired
    private ContactsMapper contactsMapper;

    @Autowired
    private ContactsRemarkMapper contactsRemarkMapper;

    @Autowired
    private ContactsActivityMapper contactsActivityMapper;

    @Autowired
    private TransactionMapper transactionMapper;

    @Autowired
    private TransactionHistoryMapper transactionHistoryMapper;

    public void getPage(Page page, Map map) {
        // 查询总记录数
        Integer totalRows = clueMapper.getCount(map);
        // 计算总页数
        Integer totalPages = (totalRows - 1) / page.getRowsPerPage() + 1;

        /*
            查询当前页的数据 limit startIndex,length
            limit 0,10  第1页
            limit 10,10  第2页
            limit 20,10  第3页
            ...
            limit (n-1)*10,10  第n页
         */
        Integer start = (page.getCurrentPage() - 1) * page.getRowsPerPage();
        Integer length = page.getRowsPerPage();
        List data = clueMapper.getData(start, length,map);

        // 把数据添加到page对象中
        page.setTotalRows(totalRows);
        page.setTotalPages(totalPages);
        page.setData(data);
    }

    public List getClueActivities(String clueId) {
        return clueActivityMapper.getByClueId(clueId);
    }

    @Override
    public void unRel(String id) {
        clueActivityMapper.delete(id);
    }

    @Override
    public void rel(String clueId, String[] actIds) {
        // 清除关系
        clueActivityMapper.delByClueId(clueId);

        if(actIds!=null && actIds.length>0) {
            List data = new ArrayList();
            for (String actId : actIds) {
                ClueActivity clueActivity = new ClueActivity();
                clueActivity.setId(UUIDUtils.getUUID());
                clueActivity.setClueId(clueId);
                clueActivity.setActivityId(actId);
                data.add(clueActivity);
            }

            clueActivityMapper.saveList(data);
        }
    }

    public Clue getClue(String id) {
        return clueMapper.get(id);
    }

    public void convert(String clueId, Transaction transaction, boolean createTran, String createBy) {

        String createTime = Constants.DateFormat.SDF19.format(new Date());

        // 查询线索信息
        Clue clue = clueMapper.get(clueId);
        // 查询线索备注
        List<ClueRemark> clueRemarks = clueRemarkMapper.getByClueId(clueId);

        // 查询线索和市场活动的关系信息
        List<ClueActivity> clueActivities = clueActivityMapper.getByClueId(clueId);


        // 创建客户信息，数据从线索中提取
        Customer customer = new Customer();
        customer.setId(UUIDUtils.getUUID());
        customer.setWebsite(clue.getWebsite());
        customer.setPhone(clue.getPhone());
        customer.setOwner(clue.getOwner());
        customer.setNextContactTime(clue.getNextContactTime());
        customer.setName(clue.getCompany());
        customer.setDescription(clue.getDescription());
        customer.setCreateTime(createTime);
        customer.setCreateBy(createBy);
        customer.setContactSummary(clue.getContactSummary());
        customer.setAddress(clue.getAddress());
        customerMapper.save(customer);


        // 创建客户备注信息，数据来自于线索备注
        if(clueRemarks!=null && clueRemarks.size()>0) {
            List customerRemarks = new ArrayList();
            for (ClueRemark clueRemark : clueRemarks) {
                CustomerRemark customerRemark = new CustomerRemark();
                // 拷贝对象属性：将clueRemark对象中的属性，拷贝到customerRemark对象中
                BeanUtils.copyProperties(clueRemark, customerRemark);
                customerRemark.setCustomerId(customer.getId());

                customerRemarks.add(customerRemark);
            }
            customerRemarkMapper.saveList(customerRemarks);
        }


        // 创建联系人，数据从线索中提取
        Contacts contacts = new Contacts();
        contacts.setId(UUIDUtils.getUUID());
        contacts.setSource(clue.getSource());
        contacts.setOwner(clue.getOwner());
        contacts.setNextContactTime(clue.getNextContactTime());
        contacts.setMphone(clue.getMphone());
        contacts.setJob(clue.getJob());
        contacts.setFullName(clue.getFullName());
        contacts.setEmail(clue.getEmail());
        contacts.setDescription(clue.getDescription());
        contacts.setCustomerId(customer.getId());
        contacts.setCreateTime(createTime);
        contacts.setCreateBy(createBy);
        contacts.setContactSummary(clue.getContactSummary());
        contacts.setAppellation(clue.getAppellation());
        contacts.setAddress(clue.getAddress());
        contactsMapper.save(contacts);


        // 创建联系人备注信息，数据来自于线索备注
        if(clueRemarks!=null && clueRemarks.size()>0) {
            List contactsRemarks = new ArrayList();
            for (ClueRemark clueRemark : clueRemarks) {
                ContactsRemark contactsRemark = new ContactsRemark();
                // 拷贝对象属性：将clueRemark对象中的属性，拷贝到customerRemark对象中
                BeanUtils.copyProperties(clueRemark, contactsRemark);
                contactsRemark.setContactsId(contacts.getId());

                contactsRemarks.add(contactsRemark);
            }
            contactsRemarkMapper.saveList(contactsRemarks);
        }


        // 创建联系人和市场活动的关联信息，数据来自线索和市场活动的关系信息
        if(clueActivities!=null && clueActivities.size()>0) {

            List<ContactsActivity> contactsActivities = new ArrayList<>();

            for (ClueActivity clueActivity : clueActivities) {
                ContactsActivity contactsActivity = new ContactsActivity();
                contactsActivity.setId(UUIDUtils.getUUID());
                contactsActivity.setActivityId(clueActivity.getActivityId());
                contactsActivity.setContactsId(contacts.getId());

                contactsActivities.add(contactsActivity);
            }

            contactsActivityMapper.saveList(contactsActivities);
        }


        if (createTran) {
            // 创建交易信息
            transaction.setContactsId(contacts.getId());
            transaction.setCustomerId(customer.getId());
            transaction.setContactSummary(clue.getContactSummary());
            transaction.setCreateBy(createBy);
            transaction.setCreateTime(createTime);
            transaction.setId(UUIDUtils.getUUID());
            transaction.setSource(clue.getSource());
            transaction.setNextContactTime(clue.getNextContactTime());
            transaction.setOwner(clue.getOwner());

            transactionMapper.save(transaction);

            // 创建交易历史信息
            TransactionHistory transactionHistory = new TransactionHistory();
            transactionHistory.setId(UUIDUtils.getUUID());
            transactionHistory.setAmountOfMoney(transaction.getAmountOfMoney());
            transactionHistory.setExpectedClosingDate(transaction.getExpectedClosingDate());
            transactionHistory.setStage(transaction.getStage());
            transactionHistory.setTransactionId(transaction.getId());

            transactionHistoryMapper.save(transactionHistory);
        }

        // 删除线索备注
        clueRemarkMapper.deleteByClueId(clueId);

        // 删除线索和市场活动的关系信息
        clueActivityMapper.delByClueId(clueId);

        // 删除线索
        clueMapper.delete(clueId);
    }
}
